{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import cv2\n",
    "import sys\n",
    "print(cv2.__version__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "OPENCV_OBJECT_TRACKERS = {\n",
    "    \"csrt\": cv2.TrackerCSRT_create,\n",
    "    \"kcf\": cv2.TrackerKCF_create,\n",
    "    \"boosting\": cv2.TrackerBoosting_create,\n",
    "    \"mil\": cv2.TrackerMIL_create,\n",
    "    \"tld\": cv2.TrackerTLD_create,\n",
    "    \"medianflow\": cv2.TrackerMedianFlow_create,\n",
    "    \"mosse\": cv2.TrackerMOSSE_create,\n",
    "    \"csrt\": cv2.TrackerCSRT_create,\n",
    "    \"goturn\": cv2.TrackerGOTURN_create\n",
    "}\n",
    "tracker_types = list(OPENCV_OBJECT_TRACKERS.keys())\n",
    "print(tracker_types)\n",
    "tracker_type = tracker_types[6]\n",
    "print(tracker_type)\n",
    "\n",
    "tracker = OPENCV_OBJECT_TRACKERS[str.lower(tracker_type)]()\n",
    "\n",
    "video = cv2.VideoCapture(0)\n",
    "if not video.isOpened():\n",
    "    exit\n",
    "\n",
    "ok, frame = video.read()\n",
    "if not ok:\n",
    "    exit\n",
    "\n",
    "bbox = cv2.selectROI(frame, False)\n",
    "\n",
    "ok = tracker.init(frame, bbox)\n",
    "while True:\n",
    "\n",
    "    ok, frame = video.read()\n",
    "    if not ok:\n",
    "        break\n",
    "\n",
    "    timer = cv2.getTickCount()\n",
    "    ok, bbox = tracker.update(frame)\n",
    "    fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer);\n",
    "\n",
    "    if ok:\n",
    "        p1 = (int(bbox[0]), int(bbox[1]))\n",
    "        p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))\n",
    "        cv2.rectangle(frame, p1, p2, (255,0,0), 2, 1)\n",
    "    else :\n",
    "        cv2.putText(frame, \"Tracking failure detected\", (100,80), cv2.FONT_HERSHEY_SIMPLEX, 0.75,(0,0,255),2)\n",
    "\n",
    "    cv2.putText(frame, tracker_type + \" Tracker\", (100,20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50),2);\n",
    "    cv2.putText(frame, \"FPS : \" + str(int(fps)), (100,50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50), 2);\n",
    "    cv2.imshow(\"Tracking\", frame)\n",
    "\n",
    "    if cv2.waitKey(1) &0xFF ==ord('q'):\n",
    "        break\n",
    "\n",
    "video.release()\n",
    "cv2.destroyAllWindows()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
